﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Guest_room.employee;
using Guest_room.BLL;

namespace Guest_room
{
    public partial class roin : Form
    {
        public roin()
        {
            InitializeComponent();
        }
        //定义适配器对象
        SqlDataAdapter dagrade;
        DataSet ds = new DataSet();
        public static string grade_id, maxnum;
        GuestRoomService guestRoomService = new GuestRoomService();

        Font f1 = new Font("UTF-8", 10);
        void init() //初始化函数
        {
            //DB.GetCn(); //建立连接
            //string str = "select * from 客房档次表"; //定义 sql语句 的查询字符串
            //dagrade = new SqlDataAdapter(str, DB.cn); //执行并返回结果
            //DB.cn.Close(); //关闭连接

            dagrade = guestRoomService.GetGuestRoomGrades();
            dagrade.Fill(ds, "gra_info");
            dataGridView1.Font = f1;
        }
        void showall()  //显示函数
        {
            DataView dvGrade = new DataView(ds.Tables["gra_info"]);
            dataGridView1.DataSource = dvGrade;
            //字体居中
            dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //列名居中
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //列宽自适应窗体，显示完整列名和数据
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            //客房设施列宽度
            dataGridView1.Columns[6].Width = 160;
            dataGridView1.Columns[5].Width = 70;
            dataGridView1.Columns[4].Width = 70;
            dataGridView1.Columns[7].Width = 90;
            //修改所有档次价格为客房价格范围
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string gradeId = dataGridView1.Rows[i].Cells["档次编号"].Value.ToString();
                string priceRange = guestRoomService.GetPriceRangeByGrade(gradeId);

                dataGridView1.Rows[i].Cells["价格"].Value = priceRange;
            }
        }
        void dvgGrade() //下拉框函数
        {
            DataView dvGrade = new DataView(ds.Tables["gra_info"]);
            comboBox1.DisplayMember = "客房档次";   //下拉框显示的值
            comboBox1.ValueMember = "客房档次";   //下拉框选中后获取的值
            comboBox1.DataSource = dvGrade;  //下拉框绑定数据源
        }
        void showXz()//显示选择列
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            //显示在第一列
            dataGridView1.Columns.Insert(0, acCode);
            //宽度为40
            dataGridView1.Columns[0].Width = 50;
        }

        private void roin_Load(object sender, EventArgs e)//窗体加载函数
        {
            label2.Text = Login.username ?? Home.username;
            init();
            dvgGrade();
            showall();
            showXz();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)//数据集多选变单选
        {
            if (dataGridView1.Rows.Count > 0)   //判断是否有数据
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)  //遍历所有行
                {
                    DataGridViewCheckBoxCell ck = dataGridView1.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;    //获取当前行的选择框
                    ck.Value = (i != e.RowIndex) ? false : true;
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) //下拉框选择查询
        {
            DataView dvGrade = new DataView(ds.Tables["gra_info"]);
            dvGrade.RowFilter = "客房档次='" + comboBox1.Text + "'";
            // dataGridView1.DataSource = dvGrade; //下拉框绑定数据源
        }

        private void button3_Click(object sender, EventArgs e)  //查询全部
        {
            showall();
        }

        private void button2_Click(object sender, EventArgs e)  //查询
        {
            DataView dvGrade = new DataView(ds.Tables["gra_info"]);
            dvGrade.RowFilter = label6.Text == "容纳人数" ? "容纳人数>='" + textBox1.Text + "'" : "客房设施 like '%" + textBox1.Text + "%'";
            dataGridView1.DataSource = dvGrade; //下拉框绑定数据源

        }

        private void label6_Click(object sender, EventArgs e)//切换显示
        {
            //点击一次切换一次
            label6.Text = label6.Text == "容纳人数" ? "客房设施" : "容纳人数";
        }

        private void button1_Click(object sender, EventArgs e)//客房信息
        {
            Default df = new Default();
            df.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Dispose();
            shcart cart = new shcart();
            cart.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)//客房信息
        {
            bool anyRowChecked = false; // 是否有任何一行被勾选
            if (dataGridView1.Rows.Count > 0)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)  //遍历所有行
                {
                    string _selectValue = dataGridView1.Rows[i].Cells["acCode"].EditedFormattedValue.ToString();// 获取选择框的值

                    if (_selectValue == "True") // 如果被勾选
                    {
                        anyRowChecked = true;   // 有行被勾选
                        grade_id = dataGridView1.Rows[i].Cells["档次编号"].Value.ToString();  // 获取当前行的产品编号
                        maxnum = dataGridView1.Rows[i].Cells["容纳人数"].Value.ToString();
                        break; // 一旦发现有勾选，则退出循环
                    }
                }
            }
            else
            {
                MessageBox.Show("没有数据");
                return;
            }
            if (!anyRowChecked || grade_id == null || grade_id == "")
            {
                MessageBox.Show("请选择一行数据");
                return;
            }
            else
            {
                this.Dispose();
                roominfo ri = new roominfo();
                ri.ShowDialog();
            }
        }
    }
}
